<?php
/*****************************************************************
 * Fusion.Board
 *
 * @author                Denis Wrobel
 * @license               http://www.gnu.org/licenses/gpl-3.0.html
 * @version               0.1
 *
 *****************************************************************/

class Forum
{
    public function fetchForums()
    {
        global $session, $db;
        
        $query = $db->query( 'SELECT c.*, f.*
                              FROM ' . TABLE_PREFIX . 'categories c
                              LEFT JOIN ' . TABLE_PREFIX . 'forums f
                              ON f.parent_category = c.category_id
                              ORDER BY c.category_order ASC, f.forum_order ASC' );
        
        $rebuild = array();
        while( $row = $db->fetch( $query ) )
        {
            $cid = $row['category_id'];
            
            if( !isset( $rebuild[ $cid ] ) )
            {
                $rebuild[ $cid ] = array( 'category_id' => $row['category_id'],
                                        'category_name' => $row['category_name'],
                                        'forums' => array() );
            }
            

            $rebuild[ $cid ]['forums'][] = array( 'forum_id' => $row['forum_id'],
                                                  'forum_name' => $row['forum_name'],
                                                  'forum_description' => $row['forum_description'],
                                                  'forum_posts' => $row['forum_posts'],
                                                  'forum_topics' => $row['forum_topics'] );
            
        }
        
        
        return $rebuild;
    }
}